package com.lazy.utils.mybatis;

import com.lazy.utils.mybatis.tk.mapper.MyMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * @author zk
 * @since  2018-05-23 15:36
 */
@Configuration
@EnableTransactionManagement
public class MybatisConfig implements EnvironmentAware {

    private Environment environment;

    /*@Bean(name = "dataSource")
    @ConfigurationProperties("druid.datasource")
    public DataSource managerEditorDataSource() {
        DruidDataSource dataSource = DataSourceBuilder.create().type(DruidDataSource.class).build();
        dataSource.setDbType(environment.getProperty("lazy.datasource.type"));
        dataSource.setUrl(environment.getProperty("lazy.datasource.url"));
        dataSource.setUsername(environment.getProperty("lazy.datasource.username"));
        dataSource.setPassword(environment.getProperty("lazy.datasource.password"));
        dataSource.setDriverClassName(environment.getProperty("lazy.datasource.driverClassName"));
        return dataSource;
    }*/

    /*@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory managerEditorSqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage(environment.getProperty("lazy.datasource.entityPackage"));

        List<Interceptor> interceptorList = new ArrayList<Interceptor>();
        String activeProfile=environment.getProperty("spring.profiles.active");
        if(ActiveEnum.DEV.getDesc().equals(activeProfile) || ActiveEnum.TEST.getDesc().equals(activeProfile)){
            //打印sql插件
            MybatisInterceptor mybatisInterceptor=new MybatisInterceptor();
            interceptorList.add(mybatisInterceptor);
        }
        //添加插件
        Interceptor[] interceptors = new Interceptor[interceptorList.size()];
        bean.setPlugins(interceptorList.toArray(interceptors));

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources(environment.getProperty("lazy.datasource.mapperLocation")));
            bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }*/

    @Bean(name = "annotationDrivenTransactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /*@Bean(name = "sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.SIMPLE);
    }*/

    @Bean(name = "mapperScannerConfigurer")
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage(environment.getProperty("lazy.datasource.daoPackage"));
        Properties properties = new Properties();
        // 这里要特别注意，不要把MyMapper放到 basePackage 中，也就是不能同其他Mapper一样被扫描到。
        properties.setProperty("mappers", MyMapper.class.getName());
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}


